<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/09/13
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Tpmecms;
use think\Db;
use think\Config;



/**
 * 物流专线🚛🚚🚚
 */
class Logistics extends Tpmecms
{

    protected $noNeedLogin = ['get_all_route','get_route_logistics_list','get_logistics_data'];
    protected $noNeedRight = ['*'];
    protected $db = 'logistics';

    public function _initialize()
    {
        parent::_initialize();
        // $user = $this->auth->getUserinfo();

        // if($this->auth->group_id!=2){
        //     $this->error('非物流专线角色');
        // }
        $data['uid'] = $this->auth->id;
        $logistics = Db::name($this->db)->where($data)->find();
        // halt($this->request->action());
        if(!$logistics && $this->request->action()!='reg_logistics'){
            $this->error('请注册物流专线');
        }
    }

    /**
     * 完善物流专线信息🚛🚚🚚
     *
     * @param string $name 物流专线名称
     * @param string $lng 经度
     * @param string $lat 纬度
     * @param string $addr 详细地址
     * @param string $tel 联系电话
     * @param string $contacts 联系人
     * @param string $logo_image 物流专线logo图片
     * @param string $images banner图
     * @param string $description 简介
     * @return void
     */
    public function reg_logistics($name=null ,$lng=null ,$lat=null ,$addr=null ,$tel=null ,$contacts=null ,$logo_image=null ,$images=null ,$description=null)
    {
      
        $data['uid'] = $this->auth->id;
        $logistics = Db::name($this->db)->where($data)->find();
        if($logistics){
            Db::name('user')->where('id',$this->auth->id)->update(['rule_status'=>'4']);
            $this->error('您已注册物流专线');
        }
        //判断是否有其他的店铺角色
        if($this->auth->rule_status>0){
            $this->error('您已有其他的店铺');
        }

        $data['name'] = $name;
        $data['lng'] = $lng;
        $data['lat'] = $lat;
        $data['addr'] = $addr;

        $data['tel'] = $tel;
        $data['contacts'] = $contacts;
        $data['logo_image'] = $logo_image;

        $data['images'] = $images;
        $data['description'] = $description;

        $data['switch'] = 1;//状态:0=不显示,1=正常显示
        $data['createtime'] = time();

        $id = Db::name($this->db)->insertGetId($data);
        if($id){
      
            $this->UpdateGeoLogistics($lng,$lat,$id);//更新物流专线定位
          
            //修改附属角色
            Db::name('user')->where('id',$this->auth->id)->update(['rule_status'=>'4']);
            $this->success('注册成功');
        }

    }

    

    /**
     * 编辑物流专线信息🚚🚚
     *
     * @param string $name 物流专线名称
     * @param string $lng 经度
     * @param string $lat 纬度
     * @param string $addr 详细地址
     * @param string $tel 联系电话
     * @param string $contacts 联系人
     * @param string $logo_image 物流专线logo图片
     * @param string $images banner图
     * @param string $description 简介
     * 
     * @return void
     */
    public function edit_logistics($name = null ,$lng = null ,$lat = null ,$addr = null ,$tel = null ,$contacts = null ,$logo_image = null ,$images = null ,$description=null)
    {
        $where['uid'] = $this->auth->id;
        $logistics = Db::name($this->db)->where($where)->find();
        if(!$logistics){
            $this->error('您没有物流专线');
        }elseif($logistics['switch']!=1){
            $this->error('您的物流专线已被关闭，请联系平台');
        }

        $data['name'] = $name;
        $data['lng'] = $lng;
        $data['lat'] = $lat;
        $data['addr'] = $addr;

        $data['tel'] = $tel;
        $data['contacts'] = $contacts;
        $data['logo_image'] = $logo_image;
        $data['images'] = $images;
        $data['description'] = $description;


        // $data['switch'] = 1;//状态:0=不显示,1=正常显示
        $data['updatetime'] = time();

        $res = Db::name($this->db)->where($where)->update($data);


        $this->UpdateGeoLogistics($lng,$lat,$logistics['id']);//更新物流专线定位

        if($res){
            $this->success('修改成功');
        }else{
            $this->error('未修改内容');
        }


    }



    /**
     * 获取我的物流专线信息🚚🚚
     *
     * @return void
     */
    public function get_my_logistics_data()
    {
     
        $logistics = $this->GetlogisticsDataByUid($this->auth->id);
        // if(!$logistics){
        //     $this->error('您没有物流专线');
        // }

        $this->success('ok',$logistics);
    }



    /**
     * 添加路线🚚🚚
     *
     * @param int $route_id 路线id
     * @return void
     */
    public function add_route($route_id)
    {
        $logistics = $this->GetlogisticsDataByUid($this->auth->id);


        $data['logistics_id'] = $logistics['id'];
        $data['route_id'] = $route_id;
        $route = Db::name('route')->find($route_id);
        if(!$route){
            $this->error('不存在该路线');
        }
        $res = Db::name('logistics_route')->where($data)->find();
        if($res){
            $this->error('您已有该路线');
        }
        Db::name('logistics_route')->insert($data);
        $this->success('添加路线成功');
    }


    /**
     * 删除路线🚚🚚
     *
     * @param int $route_id 路线id
     * @return void
     */
    public function del_route($route_id)
    {
        $logistics = $this->GetlogisticsDataByUid($this->auth->id);


        $data['logistics_id'] = $logistics['id'];
        $data['route_id'] = $route_id;

        $res = Db::name('logistics_route')->where($data)->find();
        if(!$res){
            $this->error('您没有该路线');
        }
        Db::name('logistics_route')->where($data)->delete();
        $this->success('删除路线成功');
    }


    /**
     * 获取我的路线
     *
     * @return void
     */
    public function get_my_logistics_route()
    {
        $this->success('ok',$this->GetLogisticsDataByUid($this->auth->id)['Logistics_route']);
    }


    /**
     * 获取全部路线🚚🚚
     *
     * @param int $s_area_id 出发地id(可不传)
     * @param int $e_area_id 目的地id(可不传)
     * @return void
     */
    public function get_all_route($s_area_id=null,$e_area_id=null)
    {
        $list = $this->GetAllRoute($s_area_id,$e_area_id);
        $this->success('ok',$list);
    }

    /**
     * 获取下单的订单列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     */
    public function get_my_order_list($page=1,$limit=10)
    {
        $data['uid'] = $this->auth->id;
        $logistics = Db::name($this->db)->where($data)->find();
        if(!$logistics){
            $this->error('您未注册物流专线');
        }

        $where['logistics_id'] = $logistics['id'];

        $list = Db::name('logistics_order')->where($where)->page($page,$limit)->select();
        foreach ($list as $key => &$val) {
            $val['user'] = Db::name('user')->where('id',$val['uid'])->find();
        }
        $this->success('ok',$list);

    }


    /**
     * 获取订单信息
     *
     * @param int $id id
     * @return void
     */
    public function get_order_data($id)
    {
        $val = Db::name('logistics_order')->where('id',$id)->find();
        // $val['logistics'] = $this->GetLogisticsIdData($val['logistics_id']);

        $this->success('ok',$val);
    }

    // /**
    //  * 获取物流专线信息
    //  *
    //  * @param int $logistics_id
    //  * @return void
    //  */
    // protected function GetLogisticsIdData($logistics_id)
    // {
    //     return Db::name($this->db)->find($logistics_id);
    // }


    /**
     * 获取路线的物流专线列表
     *
     * @param int $route_id 路线id
     * @param int $lat 纬度(可空)
     * @param int $lng 经度(可空)
     * 
     * @return void
     */
    public function get_route_logistics_list($route_id,$lat=null,$lng=null)
    {
        $logistics_ids = Db::name('logistics_route')->where('route_id',$route_id)->column('logistics_id');
        if(!$logistics_ids){
            $this->error('没有');
        }

        $list = Db::name($this->db)->where('id','in',$logistics_ids)->select();
        foreach ($list as $key => &$val) {
            $val['km'] = 0;
            if($lat && $lng){
                $val['km'] = round($this->get_coordinate_distance($lng,$lat,$val['lng'],$val['lat']),2);
            }
            
        }
        $this->success('ok',$list);
        


    }

    /**
     * 获取物流专线信息
     *
     * @param int $logistics_id 物流专线id
     * @return void
     */
    public function get_logistics_data($logistics_id)
    {
        $this->success('ok',$this->GetLogisticsDataById($logistics_id));
    }


}